package com.heima.wemedia.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.model.wemedia.pojo.WmNews;
import com.heima.wemedia.service.WmNewSVerfiyService;
import com.heima.wemedia.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;


@Component
public class PublishWmNews {
    @Autowired
    private WmNewsService wmNewsService;
    @Autowired
    private WmNewSVerfiyService wmNewsVerfiyService;

    /**
     * 延迟文章发布
     */
    @Scheduled(cron = "0 0/2 * * * ?")//每两分钟0秒执行一次
    public void delayPublishWmNews() {
        //查询自媒体文章表中状态为8,并且发布时间小于等于当前时间的文章
        LambdaQueryWrapper<WmNews> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmNews::getStatus, WmNews.Status.SUCCESS.getCode());
        queryWrapper.le(WmNews::getPublishTime, new Date());
        List<WmNews> news = wmNewsService.list(queryWrapper);
        if (news != null && news.size() > 0){
            //进行文章发布
            for (WmNews wmNews : news) {
                wmNewsVerfiyService.publishArticle(wmNews);
            }
        }
    }
}
